package com.study.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.Result;
import com.study.model.mdao.CustomerMapper;
import com.study.model.pojo.User;
import com.study.services.CareService;
import com.study.services.PermissionServices;
import com.study.services.UserService;

@Controller
public class LoginController {
	@Autowired
	UserService service;
	@Autowired
	CareService careService;
	@Autowired
	CustomerMapper mapper;
	@Autowired
	PermissionServices permssion;
	
	
	@PostMapping("login")
	public ModelAndView login(HttpServletRequest request, HttpSession seeesion) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username + "-" + password);
		
		ModelAndView mv = null;
		boolean result = service.findByLoginName(username);
		if (!result) {
			mv = new ModelAndView("/login");
			mv.addObject("errorname", "登录名不存在");
			return mv;
		}

		User user = service.isExsit(username, password);

		if (user == null) {
			mv = new ModelAndView("/login");
			mv.addObject("errorpassword", "登录名或者密码错误");
			return mv;

		} else {
			mv = new ModelAndView("/index");
			List<String> roleid = service.getFindUsername(user.getUseId());
			System.out.println(roleid+"               roleid");
			seeesion.setAttribute("username", user.getUseName());
			seeesion.setAttribute("useid", user.getUseId());

			
			seeesion.setAttribute("roleid", roleid);
			int count = careService.isAnn();
			mv.addObject("careCount", count);
			
			JSONArray arr=new JSONArray();
			for (int i = 0; i < roleid.size(); i++) {
				List<JSONObject> getfindPersstion = permssion.getfindPersstion(roleid.get(i).toString());
				 arr.add(getfindPersstion);
			}
			for (int i = 0; i < roleid.size(); i++) {
				if (roleid.get(i).equals("1")||roleid.get(i).equals("4")||roleid.get(i).equals("5")) {
					List<JSONObject> selectAll = mapper.selectAll(null,"aaa");
					seeesion.getServletContext().removeAttribute("mansitem");
					
					seeesion.getServletContext().setAttribute("mansitem", selectAll);
					break;
				}else if (i==roleid.size()-1&&!roleid.get(i).equals("1")&&!roleid.get(i).equals("4")&&!roleid.get(i).equals("5")) {
					List<JSONObject> selectAll = mapper.selectAll(user.getUseId(),"aaa");
					seeesion.getServletContext().removeAttribute("mansitem");
					
					seeesion.getServletContext().setAttribute("mansitem", selectAll);
				}
			}
			seeesion.setAttribute("userPersstion", arr);
			System.out.println(arr+"                    arrr");
			return mv;
		}
	}
}
